var rootPath = getRootPath();
var existRecord;
var oriPhone;
var display_style = isPhoneScreen()?"block":"flex";

$(function() {
	var rleId = getUrlParam("rleId");
	$("#rleId").val(rleId);
	//构建机构类别下拉框
//	createCategorySelect(document.getElementById("categoryId"), "2");
	//构建所在地区的下拉框
//	createProvSelect(document.getElementById("province"));
	focusById();
	//填充企业规模下拉框
//	createBsSelect(document.getElementById("bsId"));
	
	$(".cityBlock").css("display","none");
	$(".conutyBlock").css("display",display_style);
	
	$("#companyId").html("社会组织名称");
	
	  // 地址选择器遮罩层打开与关闭
	$("#areaLabel").click(function(e){
	    $("#addressSelectWrapper").show();
	    e.stopPropagation();
	});
	$(document).click(function () {
        $("#addressSelectWrapper").hide();
    });
	$("#cancel").click(function () {
        $("#addressSelectWrapper").hide();
    });
    $("#addressSelect").click(function (e) {
		e.stopPropagation();
    });
    initAddress();
	
	//反显数据
	$.ajax({
		url : rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		method : "GET",
		success : function(data) {
			if(data.returnCode == '0000') {
				if(data.returnObject != null) {
					var obj = data.returnObject;
					var sort = obj.sort;
					if (sort!=undefined && sort !='' ){
						$("#role").find("option[value='" + sort + "']").prop("selected","selected");
						$("#role").attr("disabled","true");
					}else {
						return;
					}
					
					if(sort == 20){
						/*if (obj.communityName != null
								&& obj.communityName != "") {
							$("#communityName").val(obj.communityName);
						}*/
						
						/*if (obj.location != null
								&& obj.location != "") {
							$("#Addr").val(obj.location);
						}*/
						if (obj.company != null && obj.company != "") {
							$("#company").val(obj.company);
						}
						
						if (obj.crePic != '') { 
							existRecord = true;
							$("#frontImgB").prop( "src", rootPath + "/file/viewimg?fileName=" + obj.crePic + "&type=" + 3);
							$("#fwSpanB").html("");
						}
					} else {
					/*	if (obj.communityName != null
								&& obj.communityName != "") {
							$("#communityName").val(obj.communityName);
						}*/
						if (obj.cacName != null && obj.cacName != "") {
							$("#cacName").val(obj.cacName);
						}
						if (obj.cacCode != null && obj.cacCode != "") {
							$("#cacCode").val(obj.cacCode);
						}
						if (obj.company != null && obj.company != "") {
							$("#company").val(obj.company);
						}

						if (obj.contact != null && obj.contact != "") {
							$("#contact").val(obj.contact);
						}
						if (obj.phone != null && obj.phone != "") {
							$("#phone").val(obj.phone);
							oriPhone = obj.phone;
						}
						// 微信
						if (obj.wechat != null && obj.wechat != "") {
							$("#wechat").val(obj.wechat);
						}
						if (obj.crePic != '') {
							existRecord = true;
							$("#frontImg").prop(
									"src",
									rootPath + "/file/viewimg?fileName="
											+ obj.crePic + "&type=" + 3);
							$("#fwSpan").html("");
						}
						
						var sortVal = $("#role").val();
						$("#sort").val(sortVal);
						changeRole();
					}
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});
	
	$.ajax({
		url : rootPath + "/basicInfo/getNameAndPhone",
		method : "GET",
		success : function(data) {
			if (data.returnCode == '0000') {
				if (data.returnObject != null) {
					var obj = data.returnObject;
					if (obj.realName != null && obj.realName != "") {
						 $("#nameB").val(obj.realName);
					}
					if (obj.phone != null && obj.phone != "") {
						 $("#phoneB").val(obj.phone);
					}
					 
				}
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
		}
	});

	
	setTimeout(function(){
		$("#communityName").select2({
			ajax:{
				type: 'POST',
				url: rootPath + '/solr/adreamCenterSearch',
				delay: 250,
				data: function (params) {
			          return {
			            text: params.term, // search term 请求参数
			            page: params.page
			          };
			        },
		        processResults: function (data, params) {
		            params.page = params.page || 1;
		            var srcList = data.returnCode=="0000"?data.returnObject:[];
		            var desList = [];
		            var temp;
		            for(var i=0;i<srcList.length;i++){
		            	temp = {};
//		            	temp.id=srcList[i].id;
		            	temp.id=srcList[i].title[0];
		            	temp.text=srcList[i].title[0];
		            	desList[i]=temp;
		            }
		            
		            return {
		              results: desList,//itemList
		              pagination: {
		                more: (params.page * 2) < data.total_count
		              }
		            };
		          },
		          cache: true
		        },
		        placeholder:'请选择社区',//默认文字提示
		        language: "zh-CN",
		        tags: false,//允许手动添加
		        allowClear: true,//允许清空
		        escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
		        minimumInputLength: 2,
		        formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
		        formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
		});
		},50);
	
	
	
});

function showErrMsg(id,msg){
	$("#" + id + "Tip").text(msg);
	$("#" + id + "Msg").show();
}

function hideMsg(id) {
	$("#" + id + "Msg").hide();
}

function focusById(){
	$("#communityName").focus(function(){
		hideMsg("communityName");
	});
	$("#cacName").focus(function(){
		hideMsg("cacName");
	});
	$("#cacCode").focus(function(){
		hideMsg("cacCode");
	});
	$("#company").focus(function(){
		hideMsg("company");
	});
	$("#contact").focus(function(){
		hideMsg("contact");
	});
	$("#phone").focus(function() {
		hideMsg("phone");
	});
	$("#wechat").focus(function(){
		hideMsg("wechat");
	});
	$("#Addr").focus(function(){
		hideMsg("Addr");
	});
}

function changeRole(){
	var sortVal = $("#role").val();
	hideMsg("role");
	$("#sort").val(sortVal);
	$(".city").css("cursor", "pointer");
	switch(sortVal){
		case"20":  //乡镇版社区认证
			$(".cityBlock").css("display","none");
			$(".countyBlock").css("display",display_style);
			$("#companyId").html("社会组织名称");
			break;
		case"10":  //城市版社区认证
			$(".cityBlock").css("display",display_style);
			$(".countyBlock").css("display","none");
			$("#companyId").html("机构名称");
			break;
		default:
	}
}

/**
 * 初始化地址三级联动
 * @returns
 */
function initAddress() {
    $("#Addr").cityLinkage({
        containerId: 'addressSelectWrapper',
        getSelectedCode:function(){return $("#Addr").data("code");},
        callback:function(data) {
			$("#provinceName").val(data.province.name);
			$("#cityName").val(data.city.name);
			$("#countryName").val(data.area.name);
            $("#Addr").val(data.addr).data("code",data.area.code);
        		hideMsg("Addr");
        }
    });
}